package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务人员认证审核表 服务实现类
 * </p>
 */
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {

    @Resource
    private IAgencyCertificationService agencyCertificationService;

    /**
     * 4.2.2.1 机构提交认证申请
     */
    @Override
//    @GlobelTransactional
    public void addAgencyCertification(AgencyCertificationAuditAddReqDTO agencyCertificationAuditAddReqDTO) {
        Long userId = UserContext.currentUserId();
        //1.新增或更新认证信息
        AgencyCertification agencyCertification = new AgencyCertification();
        BeanUtil.copyProperties(agencyCertificationAuditAddReqDTO, agencyCertification);
        agencyCertification.setId(userId);
        agencyCertification.setCertificationStatus(1);
        boolean save = agencyCertificationService.saveOrUpdate(agencyCertification);
        if (!save) {
            throw new RuntimeException("提交认证申请失败");
        }

        //2.提交审核信息
        //判断是新增还是更新
        LambdaQueryWrapper<AgencyCertificationAudit> qw = new LambdaQueryWrapper<>();
        qw.eq(AgencyCertificationAudit::getServeProviderId, userId);
        AgencyCertificationAudit agencyCertificationAudit = getOne(qw);
        if (BeanUtil.isEmpty(agencyCertificationAudit)) {
            agencyCertificationAudit = new AgencyCertificationAudit();
        }
        agencyCertificationAudit.setServeProviderId(userId);
        agencyCertificationAudit.setAuditStatus(0);
        boolean insert = saveOrUpdate(agencyCertificationAudit);
        if (!insert) {
            throw new RuntimeException("提交认证审核失败");
        }
    }

    /**
     * 4.2.1.2 查询最新的驳回原因
     */
    @Override
    public Map<String, String> queryRejectReason() {
        Long userId = UserContext.currentUserId();
        LambdaQueryWrapper<AgencyCertificationAudit> qw = new LambdaQueryWrapper<>();
        qw.eq(AgencyCertificationAudit::getServeProviderId, userId);
        AgencyCertificationAudit agencyCertificationAudit = getOne(qw);
        if (BeanUtil.isEmpty(agencyCertificationAudit)) {
            throw new RuntimeException("查询失败！");
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("rejectReason", agencyCertificationAudit.getRejectReason());
        return map;
    }

    /**
     * 4.2.3.3 审核机构认证分页查询
     */
    @Override
    public PageResult<AgencyCertificationAuditResDTO> pageAgencyCertificationAudit(AgencyCertificationAuditPageQueryReqDTO reqDTO) {

        //获取查询参数
        Integer auditStatus = reqDTO.getAuditStatus();
        Integer certificationStatus = reqDTO.getCertificationStatus();
        String legalPersonName = reqDTO.getLegalPersonName();
        String name = reqDTO.getName();
        //分页查询
        PageResult<AgencyCertificationAuditResDTO> pageResult = PageHelperUtils.selectPage(reqDTO, () -> baseMapper.pageAgencyCertificationAudit(auditStatus, certificationStatus, legalPersonName, name));
        return pageResult;
    }


    /**
     * 4.2.3.4 审核机构认证信息
     */
    @Override
    public void audit(Integer id, Integer certificationStatus, String rejectReason) {
        //修改认证表
        AgencyCertificationAudit agencyCertificationAudit = getById(id);
        Long serveProviderId = agencyCertificationAudit.getServeProviderId();
        boolean update = agencyCertificationService.lambdaUpdate()
                .eq(AgencyCertification::getId, serveProviderId)
                .set(AgencyCertification::getCertificationStatus, certificationStatus)
                .update();
        if (!update) {
            throw new RuntimeException("审核失败");
        }

        //修改审核表
        agencyCertificationAudit.setAuditStatus(1);
        agencyCertificationAudit.setAuditorId(UserContext.currentUserId());
        agencyCertificationAudit.setAuditorName(UserContext.currentUser().getName());
        agencyCertificationAudit.setAuditTime(LocalDateTime.now());
        agencyCertificationAudit.setRejectReason(rejectReason);
        boolean result = updateById(agencyCertificationAudit);
        if (!result) {
            throw new RuntimeException("审核失败");
        }
    }
}

